package com.zhf.converter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

import java.nio.charset.StandardCharsets;

public class SimpleStringMessageHandler implements MessageConverter {

    private static final Logger logger = LoggerFactory.getLogger(SimpleStringMessageHandler.class);

    @Override
    public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
        logger.info("to message {}", o.toString());
        return new Message(o.toString().getBytes(StandardCharsets.UTF_8), messageProperties);
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        String object = new String(message.getBody());
        logger.info("MessageConverter: from message {}", object);
        return object;
    }
}
